<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never">
      <template #header>
        <div class="justify-between">
          <!-- <el-button
            v-if="permissions.create"
            v-hasPerm="[`sys:specialaccount_payment:add`]"
            type="primary"
            :loading="download_loading"
            @click="handleCreate"
          >
            创建收支信息
          </el-button> -->
          <el-button v-if="permissions.export" type="primary" :loading="download_loading" @click="handelExport">
            导出
          </el-button>
        </div>
      </template>
      <el-table v-loading="loading" :data="tableList" highlight-current-row border>
        <el-table-column key="projectNum" label="项目编号" align="center" prop="projectNum" min-width="200" />
        <el-table-column key="projectName" label="项目名称" align="center" prop="projectName" min-width="200" />
        <el-table-column key="accountName" label="账户名称" min-width="130" align="center" prop="accountName" />
        <el-table-column key="serialNumber" label="流水号" min-width="130" align="center" prop="accountName" />
        <el-table-column key="bankNo" label="银行账号" align="center" min-width="130" prop="bankNo" />
        <el-table-column
          key="bankSerialNumber"
          label="交易流水号"
          align="center"
          prop="bankSerialNumber"
          min-width="120"
        />
        <el-table-column key="incomeTypeName" label="收支类型" min-width="80" align="center" prop="incomeTypeName" />
        <!-- <el-table-column key="tradeTypeName" label="交易类型" min-width="80" align="center" prop="tradeTypeName" /> -->
        <el-table-column key="tradeCurrency" label="交易币种" min-width="100" align="center" prop="tradeCurrency" />
        <el-table-column key="tradAbstract" label="交易摘要" min-width="80" align="center" prop="tradAbstract" />
        <el-table-column key="tradRemarks" label="交易备注" min-width="120" align="center" prop="tradRemarks" />
        <el-table-column key="balance" label="账户余额" min-width="120" align="center" prop="balance" />
        <el-table-column key="amount" label="收支金额" min-width="120" align="center" prop="amount" />
        <el-table-column key="incomeDate" label="收支日期" min-width="160" align="center" prop="incomeDate" />
        <el-table-column label="操作" fixed="right"  width="80">
          <template #default="scope">
            <!-- <div v-hasData="scope.row.hasEdit">
              <el-button
                v-if="permissions.update"
                v-hasPerm="[`sys:specialaccount_payment:update`]"
                link
                size="small"
                type="primary"
                @click="handleEdit(scope.row)"
              >
                修改
              </el-button>
              <el-button
                v-if="permissions.delete"
                v-hasPerm="[`sys:specialaccount_payment:delete`]"
                type="primary"
                link
                size="small"
                @click="handleDelete(scope.row)"
              >
                删除
              </el-button>
            </div> -->
            <el-button v-if="permissions.detail" type="primary" link size="small" @click="handleDetail(scope.row)">
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
      <add-form v-if="dialogAddOrEdit.visible" :dialog-add-or-edit="dialogAddOrEdit" @send-close="sendClose"></add-form>
      <detail v-if="dialogDetail.visible" :dialog-detail="dialogDetail" @send-close="sendClose"></detail>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import addForm from "./addForm.vue";
import detail from "./detail.vue";
import { downloadFile } from "@/utils";
import {
  QuerySpecialAccountPaymentList,
  ExportPayment,
  DeleteProjectSpecialAccountPayment,
} from "@/api/specialaccount/index";

const permissions = ref({
  create: false,
  update: false,
  delete: false,
  export: false,
  detail: false,
});

const currentSystem = ref(localStorage.getItem("usertypeName"));
const isLumpContractor = ref(localStorage.getItem("isLumpContractor"));
if (currentSystem.value == "SysUser") {
  permissions.value.create = true;
  permissions.value.update = true;
  permissions.value.delete = true;
  permissions.value.create = true;
  permissions.value.detail = true;
}

if (currentSystem.value == "Manager") {
  permissions.value.export = true;
  permissions.value.detail = true;
}
if (currentSystem.value == "Constructor" || currentSystem.value == "Commander" || currentSystem.value == "Corp") {
  permissions.value.export = true;
  permissions.value.detail = true;
}

const total = ref(0);
const loading = ref(false);
const queryFormRef = ref(ElForm);
const tableList = ref<any[]>();
const download_loading = ref(false);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  keywords: "",
});
const dialogAddOrEdit = reactive<DialogOption>({
  visible: false,
});
const dialogDetail = reactive<DialogOption>({
  visible: false,
});

const handleCreate = () => {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.footBtn = "保存";
  dialogAddOrEdit.title = "创建收支信息";
};

const handleEdit = (row: any) => {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.footBtn = "保存";
  dialogAddOrEdit.title = "修改收支信息";
  dialogAddOrEdit.id = row.id;
};

const handleDelete = (row: any) => {
  ElMessageBox.confirm("确定删除该收支信息吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    const ids = row.id;
    DeleteProjectSpecialAccountPayment(ids)
      .then((res) => {
        if (res.success) {
          ElMessage.success(res.msg);
          handleQuery();
        }
      })
      .catch((err) => {
        ElMessage.error(err.msg);
      });
  });
};

const handleDetail = (row: any) => {
  dialogDetail.visible = true;
  dialogDetail.title = "收支信息详情";
  dialogDetail.id = row.id;
};

const sendClose = function (needRefresh: boolean) {
  dialogAddOrEdit.visible = false;
  dialogAddOrEdit.id = undefined;
  dialogDetail.visible = false;
  dialogDetail.id = undefined;
  if (needRefresh) {
    handleQuery();
  }
};
/* 列表查询 */
const handleQuery = async () => {
  loading.value = true;
  let list = await QuerySpecialAccountPaymentList(queryParams);
  if (list.type === 0) {
    if (list.success) {
      tableList.value = list.data.list;
      queryParams.pageNum = list.data.pageNum;
      queryParams.pageSize = list.data.pageSize;
      total.value = list.data.total;
    } else {
      ElMessage.error(list.msg);
    }
  } else {
    ElMessage.error(list.msg);
  }
  loading.value = false;
};
const handelExport = () => {
  download_loading.value = true;
  ExportPayment(queryParams)
    .then((res) => {
      downloadFile(res);
    })
    .finally(() => {
      download_loading.value = false;
    });
};
onMounted(() => {
  handleQuery();
});
</script>

<style lang="less" scoped>
:deep(.detail .el-dialog__body) {
  height: var(--mobileAddFormHeight);
  overflow-y: auto;
}
</style>
